<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="dromara.org" />
	<meta name="description" content="dromara.org" />
	<!-- 网页标签标题 -->
	<title>dromara(Open source organization)</title>
	<link rel="shortcut icon" href="/website/img/docsite.ico"/>
	<link rel="stylesheet" href="/website/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/website/en-us/index.html"><img class="logo" src="/website/img/dromara.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">中</span><div class="header-menu"><img class="header-menu-toggle" src="/website/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/website/en-us/index.html" target="_self">Home</a><div class="sub-menu"><ul></ul></div></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="#">Document</a><div class="sub-menu"><ul><li><a href="/website/en-us/docs/soul/soul.html" target="_self">Soul</a></li><li><a href="/website/en-us/docs/hmily/index.html" target="_self">Hmily</a></li></ul></div></li><li class="menu-item menu-item-normal"><a href="/website/en-us/blog/index.html" target="_self">Blog</a><div class="sub-menu"><ul></ul></div></li><li class="menu-item menu-item-normal"><a href="/website/en-us/community/index.html" target="_self">Community</a><div class="sub-menu"><ul></ul></div></li></ul></div></div></header><section class="content-section"><div class="sidemenu" id="menu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>Soul</span><ul><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/soul.html" target="_self">Home  </a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/team.html" target="_self">Team</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/db.html" target="_self">Database Design</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/config.html" target="_self">Config</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/dataSync.html" target="_self">DataSync</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/website/en-us/docs/soul/metaData.html" target="_self">MetaData</a></li><li style="height:792px;overflow:hidden" class="menu-item menu-item-level-2"><span>User Document<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/setup.html" target="_self">Set up</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/soul-dict.html" target="_self">Dictionary management</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-handle.html" target="_self">Plugin handle explanation</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/user-http.html" target="_self">Http proxy</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/user-dubbo.html" target="_self">Dubbo proxy</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/user-springcloud.html" target="_self">Spring cloud proxy</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/user-sofa.html" target="_self">Sofa rpc proxy</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/selector.html" target="_self">Selector and Rule</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/user-dataSync.html" target="_self">Use data sync</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-divide.html" target="_self">Divide Plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-dubbo.html" target="_self">Dubbo plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-springcloud.html" target="_self">Spring cloud plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-sofa.html" target="_self">Sofa plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-rateLimiter.html" target="_self">RateLimiter plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-hystrix.html" target="_self">Hystrix plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-sentinel.html" target="_self">Sentinel plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-monitor.html" target="_self">Monitor plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-waf.html" target="_self">Waf plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-sign.html" target="_self">Sign plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-rewrite.html" target="_self">Rewrite plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/plugin-websocket.html" target="_self">Websocket plugin</a></li></ul></li><li style="height:360px;overflow:hidden" class="menu-item menu-item-level-2"><span>Developer Guide<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-filter.html" target="_self">Custom filter </a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-plugin.html" target="_self">Custom plugin</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-file.html" target="_self">File and Image</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-iphost.html" target="_self">Custom parsing IP and Host</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-result.html" target="_self">Custom result</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-sign.html" target="_self">Custom sign algorithm</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-client.html" target="_self">Developer soul client</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-thread.html" target="_self">Thread</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/dev-netty.html" target="_self">Soul optimize</a></li></ul></li><li style="height:144px;overflow:hidden" class="menu-item menu-item-level-2"><span>Community<img style="transform:rotate(0deg)" class="menu-toggle" src="/website/img/system/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/contributor.html" target="_self">Contributor</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/committer.html" target="_self">Committer</a></li><li class="menu-item menu-item-level-3"><a href="/website/en-us/docs/soul/code-conduct.html" target="_self">Code conduct</a></li></ul></li></ul></li></ul></div><div class="doc-content markdown-body" id="doc-body"><h2>description</h2>
<ul>
<li>
<p>Plugins are core executors of soul gateway. Every plugin handles matched requests when enabled.</p>
</li>
<li>
<p>There are two kinds of plugins in the soul gateway:</p>
<ul>
<li>
<p>The first type is a call chain with a single responsibility, and traffic cannot be customized.</p>
</li>
<li>
<p>The other one can do its own chain of responsibility for matched traffic.</p>
</li>
</ul>
</li>
<li>
<p>You could reference from soul-plugin module and develop plugins by yourself. Please fire pull requests of your wonderful plugins without hesitate.</p>
</li>
</ul>
<h2>single responsibility plugins</h2>
<ul>
<li>Add following dependency:</li>
</ul>
<pre><code> &lt;dependency&gt;
        &lt;groupId&gt;org.dromara&lt;/groupId&gt;
        &lt;artifactId&gt;soul-plugin-api&lt;/artifactId&gt;
        &lt;version&gt;${last.version}&lt;/version&gt;
  &lt;/dependency&gt;
</code></pre>
<ul>
<li>Declare a new class named &quot;A&quot; and implements <code>org.dromara.soul.plugin.api.SoulPlugin</code></li>
</ul>
<pre><code class="language-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">SoulPlugin</span> </span>{
    
    <span class="hljs-comment">/**
     * Process the Web request and (optionally) delegate to the next
     * {<span class="hljs-doctag">@code</span> WebFilter} through the given {<span class="hljs-doctag">@link</span> SoulPluginChain}.
     *
     * <span class="hljs-doctag">@param</span> exchange the current server exchange
     * <span class="hljs-doctag">@param</span> chain    provides a way to delegate to the next filter
     * <span class="hljs-doctag">@return</span> {<span class="hljs-doctag">@code</span> Mono&lt;Void&gt;} to indicate when request processing is complete
     */</span>
    <span class="hljs-function">Mono&lt;Void&gt; <span class="hljs-title">execute</span><span class="hljs-params">(ServerWebExchange exchange, SoulPluginChain chain)</span></span>;
    
    <span class="hljs-comment">/**
     * return plugin order .
     * This attribute To determine the plugin execution order in the same type plugin.
     *
     * <span class="hljs-doctag">@return</span> int order
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getOrder</span><span class="hljs-params">()</span></span>;
    
    <span class="hljs-comment">/**
     * acquire plugin name.
     * this is plugin name define you must offer the right name.
     * if you impl AbstractSoulPlugin this attribute not use.
     *
     * <span class="hljs-doctag">@return</span> plugin name.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> String <span class="hljs-title">named</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">""</span>;
    }
    
    <span class="hljs-comment">/**
     * plugin is execute.
     * if return true this plugin can not execute.
     *
     * <span class="hljs-doctag">@param</span> exchange the current server exchange
     * <span class="hljs-doctag">@return</span> default false.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> Boolean <span class="hljs-title">skip</span><span class="hljs-params">(ServerWebExchange exchange)</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
}

</code></pre>
<ul>
<li>
<p>Detailed instruction of interface methods:</p>
<ul>
<li>
<p><code>execute()</code> core method, you can do any task here freely.</p>
</li>
<li>
<p><code>getOrder()</code> get the order of current plugin.</p>
</li>
<li>
<p><code>named()</code> acquire the name of specific plugin.</p>
</li>
<li>
<p><code>skip()</code> determines whether this plugin should be skipped under certain conditions.</p>
</li>
</ul>
</li>
<li>
<p>Register plugin in Spring as a Bean, or simply apply <code>@Component</code> in implementation class.</p>
</li>
</ul>
<pre><code class="language-java">    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> SoulPlugin <span class="hljs-title">a</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> A();
    }
</code></pre>
<h2>Matching traffic processing plugin</h2>
<ul>
<li>Introduce the following dependency:</li>
</ul>
<pre><code class="language-xml"> <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.dromara<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>soul-plugin-base<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>${last.version}<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ul>
<li>
<p>Add a new class A, inherit from <code>org.dromara.soul.plugin.base.AbstractSoulPlugin</code></p>
</li>
<li>
<p>examples down below:</p>
</li>
</ul>
<pre><code class="language-java"><span class="hljs-comment">/**
 * This is your custom plugin.
 * He is running in after before plugin, implement your own functionality.
 * extends AbstractSoulPlugin so you must user soul-admin And add related plug-in development.
 *
 * <span class="hljs-doctag">@author</span> xiaoyu(Myth)
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CustomPlugin</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractSoulPlugin</span> </span>{
    
    <span class="hljs-comment">/**
     * return plugin order .
     * The same plugin he executes in the same order.
     *
     * <span class="hljs-doctag">@return</span> int
     */</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getOrder</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }

    <span class="hljs-comment">/**
     * acquire plugin name.
     * return you custom plugin name.
     * It must be the same name as the plug-in you added in the admin background.
     *
     * <span class="hljs-doctag">@return</span> plugin name.
     */</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">named</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">"soul"</span>;
    }

    <span class="hljs-comment">/**
     * plugin is execute.
     * Do I need to skip.
     * if you need skip return true.
     *
     * <span class="hljs-doctag">@param</span> exchange the current server exchange
     * <span class="hljs-doctag">@return</span> default false.
     */</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Boolean <span class="hljs-title">skip</span><span class="hljs-params">(<span class="hljs-keyword">final</span> ServerWebExchange exchange)</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> Mono&lt;Void&gt; <span class="hljs-title">doExecute</span><span class="hljs-params">(ServerWebExchange exchange, SoulPluginChain chain, SelectorZkDTO selector, RuleZkDTO rule)</span> </span>{
        LOGGER.debug(<span class="hljs-string">".......... function plugin start.............."</span>);
        <span class="hljs-comment">/*
         * Processing after your selector matches the rule.
         * rule.getHandle() is you Customize the json string to be processed.
         * for this example.
         * Convert your custom json string pass to an entity class.
         */</span>
        <span class="hljs-keyword">final</span> String ruleHandle = rule.getHandle();

        <span class="hljs-keyword">final</span> Test test = GsonUtils.getInstance().fromJson(ruleHandle, Test<span class="hljs-class">.<span class="hljs-keyword">class</span>)</span>;

        <span class="hljs-comment">/*
         * Then do your own business processing.
         * The last execution  chain.execute(exchange).
         * Let it continue on the chain until the end.
         */</span>
        System.out.println(test.toString());
        <span class="hljs-keyword">return</span> chain.execute(exchange);
    }
}

</code></pre>
<ul>
<li>
<p>Detailed explanation:</p>
<ul>
<li>
<p>Plugins will match the selector rule for customized plugins inherit from this abstract class. Following steps guide you to config your plugins.</p>
</li>
<li>
<p>Firstly define a new plugin in <code>soul-admin</code>, please mind that your plugin name should match the named() method overridden in your class.</p>
</li>
<li>
<p>Re-login  <code>soul-admin</code>, the plugin you added now showing on plugin-list page, you can choose selectors for matching.</p>
</li>
<li>
<p>There is a field named <code>handler</code> in rules, it is customized json string to be processed. You can process data after acquiring a ruleHandle (<code>final String ruleHandle = rule.getHandle();</code>) in <code>doExecute()</code> method.</p>
</li>
</ul>
</li>
<li>
<p>Register plugin in Spring as a Bean, or simply apply <code>@Component</code> in implementation class.</p>
</li>
</ul>
<pre><code class="language-java">    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> SoulPlugin <span class="hljs-title">a</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> A();
    }
</code></pre>
<h2>Subscribe your plugin data and do customized jobs</h2>
<ul>
<li>Declare a new class named &quot;A&quot; and implements <code>org.dromara.soul.plugin.base.handler.PluginDataHandler</code></li>
</ul>
<pre><code class="language-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">PluginDataHandler</span> </span>{
    
    <span class="hljs-comment">/**
     * Handler plugin.
     *
     * <span class="hljs-doctag">@param</span> pluginData the plugin data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handlerPlugin</span><span class="hljs-params">(PluginData pluginData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Remove plugin.
     *
     * <span class="hljs-doctag">@param</span> pluginData the plugin data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removePlugin</span><span class="hljs-params">(PluginData pluginData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Handler selector.
     *
     * <span class="hljs-doctag">@param</span> selectorData the selector data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handlerSelector</span><span class="hljs-params">(SelectorData selectorData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Remove selector.
     *
     * <span class="hljs-doctag">@param</span> selectorData the selector data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeSelector</span><span class="hljs-params">(SelectorData selectorData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Handler rule.
     *
     * <span class="hljs-doctag">@param</span> ruleData the rule data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handlerRule</span><span class="hljs-params">(RuleData ruleData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Remove rule.
     *
     * <span class="hljs-doctag">@param</span> ruleData the rule data
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeRule</span><span class="hljs-params">(RuleData ruleData)</span> </span>{
    }
    
    <span class="hljs-comment">/**
     * Plugin named string.
     *
     * <span class="hljs-doctag">@return</span> the string
     */</span>
    <span class="hljs-function">String <span class="hljs-title">pluginNamed</span><span class="hljs-params">()</span></span>;
    
}
</code></pre>
<ul>
<li>
<p>Ensure <code>pluginNamed()</code> is same as the plugin name you defined.</p>
</li>
<li>
<p>Register defined class as a Spring Bean, or simply apply <code>@Component</code> in implementation class.</p>
</li>
</ul>
<pre><code class="language-java">    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> PluginDataHandler <span class="hljs-title">a</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> A();
    }
</code></pre>
</div></section><footer class="footer-container"><div class="footer-body"><img src="/website/img/dromara.png"/><div class="cols-container"><div class="col col-12"><h3>Disclaimer</h3><p>Any unit or individual reprint all relevant information of this website, please indicate the source.</p></div><div class="col col-6"><dl><dt>Document</dt><dd><a href="/website/en-us/docs/soul/soul.html" target="_self">Overview </a></dd><dd><a href="/website/en-us/docs/soul/soul.html" target="_self">Stat up</a></dd><dd><a href="/website/en-us/docs/soul/soul.html" target="_self">Developer Guide</a></dd></dl></div><div class="col col-6"><dl><dt>Resource</dt><dd><a href="/website/en-us/blog/index.html" target="_self">Blog</a></dd><dd><a href="/website/en-us/community/index.html" target="_self">Community</a></dd></dl></div></div><div class="copyright"><span>Copyright ©2020 xiaoyu@apache.org by xiaoyu</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '/website';
  </script>
	<script src="/website/build/documentation.js"></script>
</body>
</html>